const path = require('path');
const uglify = require('uglifyjs-webpack-plugin');
const htmlPlugin = require('html-webpack-plugin');
const webpack = require('webpack');

module.exports = {
    mode: 'development',
    entry: {
        babel: ['@babel/polyfill'],
        react: ['react', 'react-dom', 'react-router', 'react-router-dom', 'redux', 'redux-thunk', 'react-redux', 'react-intl'],
        charts: ['echarts/lib/echarts'],
        rem: ['./src/app/components/rem.js',],
        main: ['./src/app/App.js'],
    },
    output: {
        path: path.resolve(__dirname, '../dist'),
        filename: '[name].bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [['@babel/preset-env', {
                            targets: {
                                edge: "17",
                                firefox: "60",
                                chrome: "67",
                                safari: "11.1",
                                ie: "9"
                            },
                            useBuiltIns: "usage",
                        }], '@babel/preset-react'],
                        plugins: [
                            '@babel/plugin-transform-runtime',
                            '@babel/plugin-transform-spread',
                            '@babel/plugin-proposal-class-properties',
                            '@babel/plugin-transform-arrow-functions',
                            ['@babel/plugin-transform-async-to-generator', {
                                'module': 'bluebird',
                                'method': 'coroutine'
                            }]
                        ]
                    }

                }
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {}
                    }
                ]
            },
            {
                test: /\.(ttf|eot|woff|woff2|svg)$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {}
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [
                    {loader: 'style-loader'},
                    {loader: 'css-loader?modules'},

                ]
            },
            {
                test: /\.less$/,
                use: [
                    {loader: 'style-loader'},
                    {loader: 'css-loader?modules'},
                    {loader: 'less-loader'},
                ]
            },
        ]
    },
    plugins: [
        new uglify(),
        new htmlPlugin({
            minify: { //是对html文件进行压缩
                removeAttributeQuotes: true  //removeAttrubuteQuotes是却掉属性的双引号。
            },
            hash: true, //为了开发中js有缓存效果，所以加入hash，这样可以有效避免缓存JS。
            template: './src/index.html', //是要打包的html模版路径和文件名称。
            favicon: './favicon.ico'
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        })
    ],
    optimization: {
        splitChunks: {
            chunks: "all",
            minSize: 30000,
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            name: true,
            cacheGroups: {
                commons: {
                    chunks: "initial",
                    test: /[\\/]node_modules[\\/]/,
                    minChunks: 2,
                    maxInitialRequests: 5, // The default limit is too small to showcase the effect
                    minSize: 0 // This is example is too small to create commons chunks
                },
                default: {
                    test: /[\\/]src[\\/]app[\\/]/,
                    minChunks: 2,//一般为非第三方公共模块
                    priority: -20,
                    reuseExistingChunk: true
                }
            }
        }
    },
    devServer: {
        //设置基本目录结构,用于找到程序打包地址
        contentBase: path.resolve(__dirname, '../dist'),
        //服务器的IP地址，可以使用IP也可以使用localhost
        host: 'localhost',
        historyApiFallback: true,
        proxy: {
            '/data': {
                target: 'http://localhost:9098/data/',
                pathRewrite: {'^/data' : ''}
            },
            '/register.do': {
                target: 'http://localhost:9098/register',
                pathRewrite: {'^/register.do' : ''}
            },
            '/login.do': {
                target: 'http://localhost:9098/login',
                pathRewrite: {'^/login.do' : ''}
            }
        },
        //服务端压缩是否开启
        compress: true,
        //配置服务端口号
        port: 8686
    }
};
